package com.letoken.platform.repo.app.manager;


import com.alibaba.fastjson.JSONObject;
import com.letoken.platform.pub.enums.LetokenTokenTypeEnum;
import com.letoken.platform.repo.admin.dao.AdminRechargeConfigMapper;
import com.letoken.platform.repo.admin.po.AdminRechargeConfig;
import com.letoken.platform.repo.app.redis.RechargeConfigTokenRedis;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author letokenlabs
 * @date 2022/11/15$
 */
@Log4j2
@Repository
public class AdminRechargeConfigManager {

    @Autowired
    private AdminRechargeConfigMapper rechargeMapper;

    public Boolean queryRechargeLocked(Integer tokenId) {
        AdminRechargeConfig rechargeConfig = queryRechargeConfig(tokenId);
        return rechargeConfig.getLocked();
    }

    public AdminRechargeConfig queryRechargeConfig(Integer tokenId) {
        AdminRechargeConfig rechargeConfig = RechargeConfigTokenRedis.me().getValue(tokenId);
        if (rechargeConfig == null) {
            rechargeConfig = rechargeMapper.queruConfigByTokenId(tokenId);
            String value = JSONObject.toJSONString(rechargeConfig);
            RechargeConfigTokenRedis.me().putValue(tokenId, value);
        }
        return rechargeConfig;
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateRecharge(Integer tokenId, AdminRechargeConfig rechargeConfig) {
        if (rechargeConfig != null) {
            rechargeConfig.updateById();
            String value = JSONObject.toJSONString(rechargeConfig);
            RechargeConfigTokenRedis.me().putValue(tokenId, value);
        }
    }

}
